package com.quanyan.stadium.biz.impl;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.google.gson.Gson;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.constants.Constants;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.*;
import com.quanyan.stadium.entity.vo.VDefinedGameSchedule;
import com.quanyan.stadium.enums.GameTypeConfigEnum;
import com.quanyan.stadium.mapper.*;
import com.quanyan.stadium.service.commonComponent.OfficialGameCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 */
@Service("bizGameSystemService")
public class BizGameSystemDaoImpl implements BizGameSystemDao {

    @Autowired
    private TbGameEventMapper tbGameEventMapper;
    @Autowired
    private TbGameRoundMapper tbGameRoundMapper;
    @Autowired
    private TbGameEventTeamMapper tbGameEventTeamMapper;
    @Autowired
    private TbGameScheduleMapper tbGameScheduleMapper;
    @Autowired
    private TbGameScheduleAddendumMapper tbGameScheduleAddendumMapper;
    @Autowired
    private TbGameRoundScoreMapper tbGameRoundScoreMapper;
    @Autowired
    private VStadiumUserDefinedMapper vStadiumUserDefinedMapper;
    @Autowired
    private TbGameOrderMapper gameOrderMapper;
    @Autowired
    private TbGameEventScoreMapper tbGameEventScoreMapper;
    @Autowired
    private TbGameClubOfficialMapper tbGameOfficialClubMapper;
    @Autowired
    private TbTeamDoubleAddendumMapper tbTeamDoubleAddendumMapper;
    @Autowired
    private TbUserGameScheduleMapper tbUserGameScheduleMapper;
    @Autowired
    private TbGameClubTeamUserMapper tbGameClubTeamUserMapper;
    @Autowired
    private TbUserFLScoreMapper tbUserFLScoreMapper;
    @Autowired
    private TbGamePosterMapper tbGamePosterMapper;

    @Autowired
    private OfficialGameCache officialGameCache;


    @Override
    public TbGameEvent insertTbGameEvent(TbGameEvent tbGameEvent) {
        tbGameEventMapper.insertSelective(tbGameEvent);
        return tbGameEvent;
    }

    @Override
    public TbGameEvent updateTbGameEvent(TbGameEvent tbGameEvent) {
        tbGameEventMapper.updateByPrimaryKeySelective(tbGameEvent);
        return tbGameEvent;
    }

    @Override
    public int countGameEventSigningByClubId(Integer clubId) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andClubIdEqualTo(clubId);
        criteria.andParentIdEqualTo(0);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andSignStartTimeLessThanOrEqualTo(new Date());
        criteria.andSignEndTimeGreaterThan(new Date());
        return tbGameEventMapper.countByExample(example);
    }

    @Override
    public int countGameEventPlayingByClubId(Integer clubId) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andClubIdEqualTo(clubId);
        criteria.andParentIdEqualTo(0);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andStartTimeLessThanOrEqualTo(new Date());
        criteria.andEndTimeGreaterThan(new Date());
        return tbGameEventMapper.countByExample(example);
    }

    @Override
    public TbGameEvent selectTbGameEvent(Integer id) {
        return tbGameEventMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbGameEvent cancelGameEvent(Integer id) {
        TbGameEvent tbGameEvent = new TbGameEvent();
        tbGameEvent.setId(id);
        tbGameEvent.setIsDelete(GameConstants.IS_DELETE);
        tbGameEvent.setCurrentRoundId(-1);
        tbGameEventMapper.updateByPrimaryKeySelective(tbGameEvent);
        //刷新缓存
        officialGameCache.delGameEvent(id);
        return tbGameEvent;
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByClubId(Integer clubId, Integer pageNum, Integer pageSize) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andClubIdEqualTo(clubId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setPage(new Page(pageSize*pageNum, pageSize));
        example.setOrderByClause("create_time desc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByClubIdNotGameType(Integer clubId, Byte gameType, Integer pageNum, Integer pageSize) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andClubIdEqualTo(clubId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andGameEventTypeNotEqualTo(gameType);
        example.setPage(new Page(pageSize*pageNum, pageSize));
        example.setOrderByClause("create_time desc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByClubId(List<Integer> clubIds) {
        if (null == clubIds || clubIds.size()<1) {
            return null;
        }
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andClubIdIn(clubIds);
        criteria.andTypeEqualTo(GameConstants.IS_DELETE);   //俱乐部发布
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setOrderByClause("id desc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByPlaceIdAndCategoryId(Integer categoryId, Integer cityId, Integer districtId, Integer placeId) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        if (null!=categoryId) {
            criteria.andCategoryIdEqualTo(categoryId);
        }
        if (null!=cityId && cityId>0) {
            criteria.andCityIdEqualTo(cityId);
        }
        if (null!=districtId&& districtId>0) {
            criteria.andDistrictIdEqualTo(districtId);
        }
        if (null!=placeId && placeId>0) {
            criteria.andPlaceIdEqualTo(placeId);
        }
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andGameEventTypeNotEqualTo((byte)GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId());
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventScoreStatus(boolean isComplemente) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andGameEventTypeNotEqualTo((byte)GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId());
        if (isComplemente) {
            criteria.andSignEndTimeGreaterThan(new Date());
            criteria.andComplementeTimeLessThanOrEqualTo(new Date());
            criteria.andRecordScoreEqualTo(GameConstants.IS_DELETE);    //生成
        }else {
            criteria.andComplementeTimeGreaterThanOrEqualTo(new Date());
            criteria.andSignStartTimeLessThanOrEqualTo(new Date());
            criteria.andRecordScoreEqualTo(GameConstants.IS_NOT_DELETE);    //未生成
        }
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByParentId(Integer parentId) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByParentIds(List<Integer> parentIds) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdIn(parentIds);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setOrderByClause("update_time desc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventByPlaceId(Integer placeId, Integer pageNum, Integer pageSize) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andGameEventTypeEqualTo((byte)GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId());
        example.setPage(new Page(pageSize*pageNum, pageSize));
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventPlaces(List<Integer> placeIds, Integer districtId, Integer categoryId) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        if (null!=placeIds) {
            criteria.andPlaceIdIn(placeIds);
        }
        criteria.andParentIdEqualTo(0);
        if (null != districtId) {
            criteria.andDistrictIdEqualTo(districtId);
        }
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andGameEventTypeEqualTo((byte)GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_PLACE.getId());
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectNotSignStartTbGameEventByPlaceIdAndCategoryId(Integer categoryId, Integer placeId) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        criteria.andParentIdEqualTo(0);
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andGameEventTypeEqualTo((byte)GameTypeConfigEnum.CONFIG_GAME_EVENT_TYPE_NGKC.getId());
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andSignStartTimeGreaterThan(new Date());
        example.setOrderByClause("start_time asc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public int countTbGameEventByClubIdNotGameType(Integer clubId, Byte gameType) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andClubIdEqualTo(clubId);
        criteria.andGameEventTypeNotEqualTo(gameType);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameEventMapper.countByExample(example);
    }

    @Override
    public TbGameRound insertTbGameRound(TbGameRound tbGameRound) {
        tbGameRoundMapper.insertSelective(tbGameRound);
        return tbGameRound;
    }

    @Override
    public TbGameRound updateTbGameRound(TbGameRound tbGameRound) {
        tbGameRoundMapper.updateByPrimaryKeySelective(tbGameRound);
        return tbGameRound;
    }

    @Override
    public List<TbGameRound> selectTbGameRoundByGameEventId(Integer gameEventId) {
        TbGameRoundExample example = new TbGameRoundExample();
        TbGameRoundExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setOrderByClause("id asc");
        return tbGameRoundMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEventTeam> selectMyTbGameEventTeam(Integer uid, List<Integer> clubIds, Byte teamType) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(teamType);
        if (teamType.intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            criteria.andClubIdIn(clubIds);
        }else {
            criteria.andUidEqualTo(uid);
        }
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEventTeam> selectTbGameEventTeam(List<Integer> idList, Byte teamType, Integer gameEventId){
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(teamType);
        criteria.andGameEventIdEqualTo(gameEventId);
        if (teamType.intValue() == GameConstants.TEAM_TYPE_CLUB.intValue()) {
            criteria.andClubIdIn(idList);
        }else {
            criteria.andUidIn(idList);
        }

        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public List<TbGameRound> selectTbGameRoundByGameEventIdAndGenerate(Integer gameEventId) {
        TbGameRoundExample example = new TbGameRoundExample();
        TbGameRoundExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        criteria.andIsGenerateEqualTo((byte) 1);
        return tbGameRoundMapper.selectByExample(example);
    }

    @Override
    public List<TbGameRound> selectTbGameRoundByGameEventIdAndGenerateNotCur(Integer gameEventId) {
        TbGameRoundExample example = new TbGameRoundExample();
        TbGameRoundExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameRoundMapper.selectByExample(example);
    }


    @Override
    public TbGameRound selectTbGameRound(Integer id) {
        return tbGameRoundMapper.selectByPrimaryKey(id);
    }

    @Override
    public TbGameEventTeam insertTbGameEventTeam(TbGameEventTeam tbGameEventTeam) {
        tbGameEventTeamMapper.insertSelective(tbGameEventTeam);
        return tbGameEventTeam;
    }

    @Override
    public TbGameEventTeam updateTbGameEventTeam(TbGameEventTeam tbGameEventTeam) {
        tbGameEventTeamMapper.updateByPrimaryKeySelective(tbGameEventTeam);
        return tbGameEventTeam;
    }

    @Override
    public List<TbGameEventTeam> selectTbGameEventTeamByGameEventId(Integer gameEventId, Integer pageNum, Integer pageSize, Byte isDelete) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        if (null!=isDelete) {
            criteria.andIsDeleteEqualTo(isDelete);
        }
        if (pageNum != null && pageSize != null) {
            example.setPage(new Page(pageNum * pageSize, pageSize));
        }
        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public TbGameEventTeam selectTbGameEventTeamById(Integer id) {
        return tbGameEventTeamMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbGameEventTeam> selectTbGameEventTeamByIds(List<Integer> ids) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        Gson gson = new Gson();
        String tempStr = "id,'"+gson.toJson(ids).replace("[", "").replace("]", "") +"'";
        example.setOrderByClause("FIND_IN_SET("+ tempStr +")");
        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public int countTbGameEventTeamByGameEventId(Integer gameEventId) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(Constants.IS_DELETE_FALSE);
        return tbGameEventTeamMapper.countByExample(example);
    }

    @Override
    public TbGameSchedule insertTbGameSchedule(TbGameSchedule tbGameSchedule) {
        tbGameScheduleMapper.insertSelective(tbGameSchedule);
        return tbGameSchedule;
    }

    @Override
    public TbGameSchedule updateTbGameSchedule(TbGameSchedule tbGameSchedule) {
        tbGameScheduleMapper.updateByPrimaryKeySelective(tbGameSchedule);
        return tbGameSchedule;
    }

    @Override
    public List<TbGameSchedule> selectTbGameScheduleByGameRoundId(Integer gameRoundId, Integer pageNum, Integer pageSize) {
        TbGameScheduleExample example = new TbGameScheduleExample();
        TbGameScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdEqualTo(gameRoundId);
        if (pageNum != null && pageSize != null) {
            example.setPage(new Page(pageNum * pageSize, pageSize));
        }
        return tbGameScheduleMapper.selectByExample(example);
    }

    @Override
    public List<TbGameSchedule> selectTbGameScheduleByGameRoundId(Integer gameRoundId) {
        TbGameScheduleExample example = new TbGameScheduleExample();
        TbGameScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdEqualTo(gameRoundId);

        return tbGameScheduleMapper.selectByExample(example);
    }

    @Override
    public List<VDefinedGameSchedule> selectVDefinedGameScheduleByGameRoundId(Integer gameRoundId, Integer pageNum, Integer pageSize) {

        if (pageNum == null || pageSize == null || pageNum == 0) {
            pageNum=0;
            pageSize = GameConstants.GAME_SCHEDULE_NUM;
        }
        return vStadiumUserDefinedMapper.selectVDefinedGameScheduleList(gameRoundId, pageNum*pageSize, pageSize);
    }

    @Override
    public List<VDefinedGameSchedule> selectVDefinedGameScheduleById(Integer gameScheduleId) {
        return vStadiumUserDefinedMapper.selectVDefinedGameScheduleById(gameScheduleId);
    }

    @Override
    public TbGameRoundScore insertTbGameRoundScore(TbGameRoundScore tbGameRoundScore) {
        tbGameRoundScoreMapper.insertSelective(tbGameRoundScore);
        return tbGameRoundScore;
    }

    @Override
    public TbGameRoundScore updateTbGameRoundScore(TbGameRoundScore tbGameRoundScore) {
        tbGameRoundScoreMapper.updateByPrimaryKeySelective(tbGameRoundScore);
        return tbGameRoundScore;
    }

    @Override
    public List<TbGameRoundScore> selectTbGameRoundScoreByGameRoundId(Integer gameRoundId, Integer pageNum, Integer pageSize) {

        TbGameRoundScoreExample example = new TbGameRoundScoreExample();
        TbGameRoundScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdEqualTo(gameRoundId);
        if (pageNum != null && pageSize != null) {
            example.setPage(new Page(pageNum * pageSize, pageSize));
        }
        example.setOrderByClause("score desc, game_event_team_id asc");
        return tbGameRoundScoreMapper.selectByExample(example);
    }

    @Override
    public List<TbGameRoundScore> selectTbGameRoundScoreByGameRoundId(List<Integer> gameRoundIdList, Integer pageNum, Integer pageSize) {
        TbGameRoundScoreExample example = new TbGameRoundScoreExample();
        TbGameRoundScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdIn(gameRoundIdList);

        if (pageNum != null && pageSize != null) {
            example.setPage(new Page(pageNum * pageSize, pageSize));
        }
        example.setOrderByClause("score desc, game_event_team_id asc");
        return tbGameRoundScoreMapper.selectByExample(example);
    }

    @Override
    public List<TbGameRoundScore> selectTbGameRoundScoreByGameRoundIds(List<Integer> gameRoundIds){
        TbGameRoundScoreExample example = new TbGameRoundScoreExample();
        TbGameRoundScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdIn(gameRoundIds);
        return tbGameRoundScoreMapper.selectByExample(example);
    }

    @Override
    public TbGameRoundScore selectTbGameRoundScoreByGameRoundIdAndTeamId(Integer gameRoundId, Integer teamId) {
        TbGameRoundScoreExample example = new TbGameRoundScoreExample();
        TbGameRoundScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdEqualTo(gameRoundId);
        criteria.andGameEventTeamIdEqualTo(teamId);
        List<TbGameRoundScore> scores = tbGameRoundScoreMapper.selectByExample(example);
        if (null!=scores && scores.size() == 1) {
            return scores.get(0);
        }
        return null;
    }

    @Override
    public List<TbGameScheduleAddendum> selectTbGameScheduleAddendumByGameScheduleId(Integer gameScheduleId) {
        TbGameScheduleAddendumExample example = new TbGameScheduleAddendumExample();
        TbGameScheduleAddendumExample.Criteria criteria = example.createCriteria();
        criteria.andGameScheduleIdEqualTo(gameScheduleId);
        return tbGameScheduleAddendumMapper.selectByExample(example);
    }

    @Override public boolean updateGameStorage(Integer id, int num) {
        return vStadiumUserDefinedMapper.updateStadiumAmount(id,num) > 0;
    }

    @Override public TbGameOrder insertGameOrder(TbGameOrder tbGameOrder) {
        gameOrderMapper.insertSelective(tbGameOrder);
        return tbGameOrder;
    }

    @Override public TbGameOrder updateTbGameOrder(TbGameOrder tbGameOrder) {
        gameOrderMapper.updateByPrimaryKeySelective(tbGameOrder);
        return tbGameOrder;

    }

    @Override
    public void insertTbGameEventScore(TbGameEventScore tbGameEventScore) {
        tbGameEventScoreMapper.insertSelective(tbGameEventScore);
    }

    @Override
    public  List<TbGameEventTeam> selectTbGameEventTeamByGameEventId(Integer gameEventId, Byte isDelete) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        if (null != isDelete) {
            criteria.andIsDeleteEqualTo(isDelete);
        }
        example.setOrderByClause("score desc,id asc");
        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public List<TbGameRound> selectProceedsGameRoundByGameRoundid(Integer roundid) {
        TbGameRoundExample example = new TbGameRoundExample();
        TbGameRoundExample.Criteria criteria = example.createCriteria();
        criteria.andIdLessThan(roundid);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameRoundMapper.selectByExample(example);
    }

    public List<TbGameClubOfficial> queryTbGameOfficialClubList(Integer clubId, Integer categoryId) {
        TbGameClubOfficialExample example = new TbGameClubOfficialExample();
        TbGameClubOfficialExample.Criteria criteria = example.createCriteria();
        if (null!=clubId) {
            criteria.andClubIdEqualTo(clubId);
        }
        if (null!=categoryId) {
            criteria.andCategoryIdEqualTo(categoryId);
        }
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGameOfficialClubMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEventScore> queryTbGameEventScoreList(Integer gameEventId) {
        TbGameEventScoreExample example = new TbGameEventScoreExample();
        TbGameEventScoreExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setOrderByClause("rank asc");
        return tbGameEventScoreMapper.selectByExample(example);
    }

    @Override
    public List<TbTeamDoubleAddendum> queryTbTeamDoubleAddendumList(Integer gameEventId) {
        TbTeamDoubleAddendumExample example = new TbTeamDoubleAddendumExample();
        TbTeamDoubleAddendumExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbTeamDoubleAddendumMapper.selectByExample(example);
    }

    @Override
    public void updateUserGameScheduleFinish(Integer gameScheduleId, String score, Integer uid, boolean isAdmin, Byte isWin) {
        TbUserGameSchedule tbUserGameSchedule = new TbUserGameSchedule();
        tbUserGameSchedule.setRecordAuth(GameConstants.IS_DELETE);
        tbUserGameSchedule.setIsWin(isWin);
        if (isAdmin) {
            tbUserGameSchedule.setScheduleStatus(GameConstants.IS_DELETE);
            tbUserGameSchedule.setScheduleScore(score);
        }
        TbUserGameScheduleExample example = new TbUserGameScheduleExample();
        TbUserGameScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andGameScheduleIdEqualTo(gameScheduleId);
        if (!isAdmin) {
            criteria.andUidEqualTo(uid);
        }
        tbUserGameScheduleMapper.updateByExampleSelective(tbUserGameSchedule, example);
    }


    @Override
    public List<TbGameEvent> gameEventSearch(Map<String, Integer> searchMap) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andCityIdEqualTo(searchMap.get("cityId"));
        if (searchMap.containsKey("districtId")) {
            criteria.andDistrictIdEqualTo(searchMap.get("districtId"));
        }

        if (searchMap.containsKey("categoryId")) {
            criteria.andCategoryIdEqualTo(searchMap.get("categoryId"));
        }

        if (searchMap.containsKey("gameSystemType")) {
            criteria.andGameSystemTypeEqualTo(searchMap.get("gameSystemType").byteValue());
        }

        if (searchMap.containsKey("type")) {
            criteria.andTypeEqualTo(searchMap.get("type").byteValue());
        } else {
            criteria.andTypeNotEqualTo(GameConstants.PERSONAL_CREATE);
        }

        if (searchMap.containsKey("gameMinLevel") && searchMap.containsKey("gameMaxLevel")) {
            criteria.andGameGradeLevelTypeBetween(searchMap.get("gameMinLevel").byteValue(), searchMap.get("gameMaxLevel").byteValue());
        }

        if (searchMap.containsKey("gameLevel")) {
            criteria.andGameGradeLevelTypeEqualTo(searchMap.get("gameLevel").byteValue());
        }

        // 已结束的赛事不做显示
        criteria.andCurrentRoundIdNotEqualTo(-1);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setPage(new Page(searchMap.get("pageSize")*searchMap.get("pageNo"), searchMap.get("pageSize")));
        example.setOrderByClause("start_time desc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public void initTbGameClubTeamUser(Integer gameEventId, Integer clubId, List<Integer> uids, Integer gameTeamId) {
        if (CollectionUtils.isNotEmpty(uids)) {
            for (Integer integer : uids) {
                TbGameClubTeamUser user = new TbGameClubTeamUser();
                user.setGameEventId(gameEventId);
                user.setClubId(clubId);
                user.setUid(integer);
                user.setGameTeamId(gameTeamId);
                tbGameClubTeamUserMapper.insertSelective(user);
            }
        }
    }

    @Override
    public List<TbGameClubTeamUser> queryTbGameClubTeamUserByClubId(Integer gameEventId, Integer clubId) {
        TbGameClubTeamUserExample example = new TbGameClubTeamUserExample();
        TbGameClubTeamUserExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        if (null!=clubId) {
            criteria.andClubIdEqualTo(clubId);
        }
        return tbGameClubTeamUserMapper.selectByExample(example);
    }

    @Override
    public List<TbGameClubTeamUser> queryTbGameClubTeamUserByUid(Integer gameEventId, Integer uid) {
        TbGameClubTeamUserExample example = new TbGameClubTeamUserExample();
        TbGameClubTeamUserExample.Criteria criteria = example.createCriteria();
        criteria.andGameEventIdEqualTo(gameEventId);
        criteria.andUidEqualTo(uid);
        return tbGameClubTeamUserMapper.selectByExample(example);
    }

    @Override
    public List<TbUserFLScore> queryTbUserFLScoreByUid(List<Integer> gameScheduleIds, Integer uid) {
        TbUserFLScoreExample example = new TbUserFLScoreExample();
        TbUserFLScoreExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);
        criteria.andGameScheduleIdIn(gameScheduleIds);
        example.setOrderByClause("game_schedule_id asc");
        return tbUserFLScoreMapper.selectByExample(example);
    }

    @Override
    public List<TbUserGameSchedule> queryTbUserGameScheduleByUid(Integer gameEventId,Integer uid) {
        TbUserGameScheduleExample example = new TbUserGameScheduleExample();
        TbUserGameScheduleExample.Criteria criteria = example.createCriteria();
        if (null!=uid) {
            criteria.andUidEqualTo(uid);
        }
        if (null!=gameEventId) {
            criteria.andGameEventIdEqualTo(gameEventId);
        }
        return tbUserGameScheduleMapper.selectByExample(example);
    }

    @Override
    public int updateTbUserGameScheduleByUid(TbUserGameSchedule tbUserGameSchedule) {
        return tbUserGameScheduleMapper.updateByPrimaryKeySelective(tbUserGameSchedule);
    }

    @Override
    public TbGameSchedule selectTbGameScheduleById(Integer gameScheduleId) {
        return tbGameScheduleMapper.selectByPrimaryKey(gameScheduleId);
    }

    @Override
    public int deleteTbGameScheduleAddendumByGameScheduleId(List<Integer> gameScheduleIds) {
        TbGameScheduleAddendumExample example = new TbGameScheduleAddendumExample();
        TbGameScheduleAddendumExample.Criteria criteria = example.createCriteria();
        criteria.andGameScheduleIdIn(gameScheduleIds);

        return tbGameScheduleAddendumMapper.deleteByExample(example);
    }

    @Override
    public void insertTbGameScheduleAddendum(TbGameScheduleAddendum tbGameScheduleAddendum) {
        tbGameScheduleAddendumMapper.insert(tbGameScheduleAddendum);
    }

    @Override
    public List<TbGameScheduleAddendum> selectTbGameScheduleAddendumByGameScheduleIds(List<Integer> gameScheduleIds) {
        TbGameScheduleAddendumExample example = new TbGameScheduleAddendumExample();
        TbGameScheduleAddendumExample.Criteria criteria = example.createCriteria();
        criteria.andGameScheduleIdIn(gameScheduleIds);

        criteria.andCreateTimeEqualTo(new Date());
        criteria.andUpdateTimeEqualTo(new Date());

        return tbGameScheduleAddendumMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEvent> selectTbGameEventList(Map<String, Object> parmMap) {
        TbGameEventExample example = new TbGameEventExample();
        TbGameEventExample.Criteria criteria = example.createCriteria();
        criteria.andCityIdEqualTo((Integer) parmMap.get("cityId"));
        if (parmMap.containsKey("districtId")) {
            criteria.andDistrictIdEqualTo((Integer) parmMap.get("districtId"));
        }

        if (parmMap.containsKey("categoryId")) {
            criteria.andCategoryIdEqualTo((Integer)parmMap.get("categoryId"));
        }

        if (parmMap.containsKey("startTime")) {
            criteria.andStartTimeGreaterThanOrEqualTo((Date)parmMap.get("startTime"));
        }

        if (parmMap.containsKey("gameType")) {
            criteria.andGameEventTypeEqualTo((byte)parmMap.get("gameType"));
        }

        if (parmMap.containsKey("endTime")) {
            criteria.andStartTimeLessThan((Date)parmMap.get("endTime"));
        }

        // 已结束的赛事不做显示
        criteria.andCurrentRoundIdNotEqualTo(-1);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);

        int pageSize = (Integer) parmMap.get("pageSize");
        int pageNo = (Integer)parmMap.get("pageNo");
        example.setPage(new Page(pageSize * pageNo, pageSize));

        example.setOrderByClause("start_time desc");
        return tbGameEventMapper.selectByExample(example);
    }

    @Override
    public List<TbGameEventTeam> selectUidTbGameEventList(Integer uid, Integer pageNo, Integer pageSize) {
        TbGameEventTeamExample example = new TbGameEventTeamExample();
        TbGameEventTeamExample.Criteria criteria = example.createCriteria();
        criteria.andUidEqualTo(uid);

        // 已删除的赛事不做显示
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        example.setPage(new Page(pageSize * pageNo, pageSize));
        example.setOrderByClause("id desc");
        return tbGameEventTeamMapper.selectByExample(example);
    }

    @Override
    public List<TbGamePoster> queryTbGamePosterList(Integer categoryId) {
        TbGamePosterExample example = new TbGamePosterExample();
        TbGamePosterExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
        return tbGamePosterMapper.selectByExample(example);
    }

    @Override
    public int updateTbGameScheduleByGameRoundId(String lastTeamListIndex,Integer gameRoundId) {
        TbGameSchedule tbGameSchedule = new TbGameSchedule();
        tbGameSchedule.setLastTeamListIndex(lastTeamListIndex);
        TbGameScheduleExample example = new TbGameScheduleExample();
        TbGameScheduleExample.Criteria criteria = example.createCriteria();
        criteria.andGameRoundIdEqualTo(gameRoundId);

        return  tbGameScheduleMapper.updateByExampleSelective(tbGameSchedule, example);
    }
}
